﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

using EventBrokR;

namespace ERPStore.Exporter
{
	public class PluginInit : ERPStore.Extensibility.PluginInit
	{
		public PluginInit()
		{
			Container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
		}

		protected Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public override void AddServices()
		{
			if (Configuration.ConfigurationSettings.AppSettings["repositoryName"] == "mock")
			{
				// For tests
				Container.RegisterType<CloudSoft.Repositories.IRepository<Repositories.ExporterDbContext>, CloudSoft.Repositories.MemoryRepository<Repositories.ExporterDbContext>>(new ContainerControlledLifetimeManager());
			}
			else
			{
				Container.RegisterType<CloudSoft.Repositories.IDbContextFactory<Repositories.ExporterDbContext>, CloudSoft.Repositories.DbContextFactory<Repositories.ExporterDbContext>>(new ContainerControlledLifetimeManager());
				Container.RegisterType<CloudSoft.Repositories.IRepositoryInitializer, CloudSoft.Repositories.Initializers.SqlSchemaInitializer<Repositories.ExporterDbContext>>("exporterschemainitializer", new PerResolveLifetimeManager());
				Container.RegisterType<CloudSoft.Repositories.IRepository<Repositories.ExporterDbContext>, CloudSoft.Repositories.SqlRepository<Repositories.ExporterDbContext>>(new ContainerControlledLifetimeManager());
			}

			Container.RegisterType<Services.IExporterService, Services.ExporterService>(new ContainerControlledLifetimeManager());
			Container.RegisterType<Services.ShopzillaService>(new ContainerControlledLifetimeManager());

			// Register export providers
			Container.RegisterType<Providers.IExport, Providers.CherchonsExport>("cherchons", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.AchetezFacileExport>("achetezfacile", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.GoogleShoppingExport>("googleshopping", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.IdealoExport>("idealo", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.LeguideExport>("leguide", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.ShopzillaExport>("shopzilla", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.SitemapExport>("sitemap", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.HelloproExport>("hellopro", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.NetAffiliationExport>("netaffiliation", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.TwengaExport>("twenga", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.CriteoExport>("criteo", new PerResolveLifetimeManager());
			Container.RegisterType<Providers.IExport, Providers.TouslesprixExport>("touslesprix", new PerResolveLifetimeManager());

			if (ERPStore.GlobalConfiguration.Configuration.HosterName != "WebApp"
				|| ERPStore.GlobalConfiguration.Configuration.ExecutingContext == ERPStore.Models.ExecutingContext.Test)
			{
				var eb = Container.Resolve<EventBrokR.IPublisher>();
				// EventConsumer
				eb.Container.Register<EventConsumers.BanishProductEventConsumer>();
			}
		}

		public override void Load()
		{
			if (Configuration.ConfigurationSettings.AppSettings["repositoryName"] != "mock")
			{
				var initializer = Container.Resolve<CloudSoft.Repositories.IRepositoryInitializer>("exporterschemainitializer");
				if (initializer != null)
				{
					initializer.Initialize("_erpstore_exporter_schema");
				}
			}

			var taskService = Container.Resolve<ERPStore.Services.IScheduledTaskService>();
			if (ERPStore.GlobalConfiguration.Configuration.HosterName == "WebApp")
			{
				taskService.Add(TaskHelper.CreateCatalogsTask());
			}
			else
			{
				taskService.Add(TaskHelper.CreateBanishedProductCleanerTask());
			}

			var leadSourceRegistrationService = Container.Resolve<ERPStore.Services.ILeadSourceRegistrationService>();
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.CherchonsSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.AchetezFacileSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.GoogleShoppingSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.IdealoSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.LeGuideSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.ShopzillaSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.HelloproSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.NetAffiliationSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.TwengaSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.CriteoSource());
			leadSourceRegistrationService.RegisterLeadSource(new Models.LeadSources.TouslesprixSource());

			if (GlobalConfiguration.Configuration.ExecutingContext != ERPStore.Models.ExecutingContext.Test)
			{
				Services.ShopzillaService.GererateTaxonomy();
			}
		}


	}
}
